<?php
namespace Yzadmin\Controller;
use Think\Controller;
//name:商品模块
//data:2015-12-3
//user:ChenFei

class ShopController extends CkController
{
    //商品管理
    public function index()
    {   
        $data['list']= M('shop_item')->select();
        $data['title']="商品列表";
        $this->assign($data);
        $this->display();    
    }

    //商品添加
    public function add_item()
    {
        $data['title']="商品添加";
        $this->assign($data);
        $this->display();
    }
    

    //商品添加处理
    public function add_update()
    {
        $data=I('post.');
        $data['suk']=empty_array($data['suk']);
        $data['tuwen']=empty_array($data['tuwen']);
        $rules = array(   
           array('title','require','请填写商品标题！'), 
           array('litpic','require','请上传商品缩略图！'), 
        );
        $shop_item = M("shop_item"); // 实例化User对象
        if (!$shop_item->validate($rules)->create()){    
             $this->error($shop_item->getError());
             die();
           }
        $_shop_item=$shop_item->where(array('title'=>$data['title']))->find();
        if(!empty($_shop_item) || $data['id'])
        {  
           if($_shop_item){
            $where= array("id"=>$_shop_item['id']);
           }else{
              $where= array("id"=>$data['id']);
           }
           //这里为更新
           if($shop_item->where($where)->save($data)!==false)
           {
             $this->success("商品更新成功!",U("Shop/index"));
           }
        }else{ 
           //这里为添加文章
           if($shop_item->add($data)!==false)
           {
            $this->success("商品添加成功!",U("Shop/index"));
           }
        }
    }


    //商品编辑
    public function update_item()
    {   
        $id=I('get.id');
        $data['shop_item'] = M("shop_item")->where("id=".$id)->find();
        $data['title']="商品编辑";
        $this->assign($data);
        $this->display();
    }
    

 
    

    //商品删除
    public function dels_item()
    {
      $id= I("get.id");
      if(!$id){ $this->error("请选择要删除的商品!");}
      if(M("shop_item")->where("id=".$id)->delete()!==false)
      {
        $this->success("删除成功!",U("Shop/index"));
      }else{
        $this->error("删除失败!",U("Shop/index"));
      }      
    }
    
    

    //软文管理
    public function news()
    {
        $p=I('get.p')?(int)I('get.p'):0;//当前页数
        $cid=I('get.cid')?(int)I('get.cid'):'';
        $num=10; //每页调用条数
        //$where=array("news.look"=>0,); //调用没有锁定文章
        if($cid){$where["cid"]=$cid;} //判断有没有选择分类id
        $newslist=D("shop_news"); //实例化视图模型
        $count=$newslist->where($where)->count();//计算总数
        $data['newslist']=$newslist->where($where)->order("sort asc,id desc")->page($p.','.$num)->select();
        $Page= new \Think\Page($count,$num);// 实例化分页类 传入总记录数和每页显示的记录数
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $data["cate"]=M("shop_item")->select();  //调用商品
        $data['page']= $Page->show();// 分页显示输出
        $data['title']="商品软文列表";
        $this->assign($data);
        $this->display();       
    }
    

    //增加软文
    public function news_add()
    {   
        $data['cate']=M("shop_item")->select(); //得到所有分类
        $data['yumi']=M("yumi")->where(array("look"=>0,'type'=>2))->order("Rand()")->select();  
        $data['title']="增加软文";
        $this->assign($data);
        $this->display(); 
    }


    //文章更新和增加
    public function news_add_update()
    {
        $data=I('post.');
        $data=self::datas($data);
        $rules = array(   
           array('title','require','请填写标题！'),
        );
        $shop_news = M("shop_news"); // 实例化User对象
        if (!$shop_news->validate($rules)->create()){    
             $this->error( $shop_news->getError());
             die();
           }
        $_shop_news= $shop_news->where(array('title'=>$data['title']))->find();
        if(!empty($_shop_news) || $data['id'])
        {
           //这里为更新
           if($shop_news->where(array("id"=>$_shop_news['id']))->save($data)!==false)
           {
             $this->success("软文更新成功!",U("Shop/news"));
           }
        }else{ 
           //这里为添加文章
           if($shop_news->add($data)!==false)
           {
            $this->success("软文添加成功!",U("Shop/news"));
           }
        }  
     }


    //对是有内容数据进行处理
    private function datas($data)
    {  
       $data['time']=date('Y-m-d');
       if(empty($data['yumi_id'])){ 
         $yumi=M("yumi")->where("look=0")->field("id")->order("Rand()")->find();
         $data['yumi_id']=$yumi['id'];
       }
       if(empty($data['click'])){
         $data['click']=rand(4019,19918);
       } 
       return $data;
    }


    //编辑软文
    public function news_update()
    {
        $id=I('get.id');
        $data['news']=M("shop_news")->find($id);
        $data['cate']=M("shop_item")->select(); //得到所有分类
        $data['yumi']=M("yumi")->where(array("look"=>0,'type'=>2))->order("Rand()")->select(); //得到所有部署域名iD这里采集了一个随机排序
        $data['title']="软文修改";
        $this->assign($data);
        $this->display();
    }
    

    //订单管理
    public function order()
    {
       $type=I('get.type') ? I('get.type'): 1;
       if(!I('get.select') || !I('get.nr')){
          $where=array("type"=>$type);
        }else{
          $get=I("get.");
          $where=array($get['select']=>array('like','%'.$get['nr'].'%'));
       }
       $num=10;
       $p=I('get.p')?(int)I('get.p'):0;//当前页数
       $data['order_list']=M('shop_order')->where($where)->page($p.','.$num)->select();
       $count=M('shop_order')->where($where)->count();//计算总数
       $Page= new \Think\Page($count,$num);// 实例化分页类 传入总记录数和每页显示的记录数
       $Page->setConfig('prev','上一页');
       $Page->setConfig('next','下一页');
       $data['page']= $Page->show();// 分页显示输出
       $data['title']="订单管理";
       $this->assign($data);
       $this->display();
    }
    
    
    //订单详细
    public function order_show()
    {
       if(IS_POST){
          $data=I('post.');
          if(!$data['type']) $this->error("已结算,不可在操作!");
          if($data['type']==3)
          {
             if(empty($data['com']) || empty($data['nu'])){
               $this->error("快递名称和快递单号不可以为空!");
             }
          }
          if($data['type']==7){
             if(empty($data['content'])) $this->error("请填写备注!");
          }
          $order=M('shop_order')->where('id='.$data['id'])->find();
          if($order['type']==4 || $order==5){
             $this->error("已结算,不可在操作!");
          }
          if(M('shop_order')->where(array('id'=>$data['id']))->save($data)!==false){
             
             if($data['type']==4){
                $this->user_ticheng($order);
             }else{
                $this->success("状态更新成功!"); 
             }

          }else{
             $this->error("状态异常");
          }
       }
       $id= I('get.id');
       $data['order']= M('shop_order')->where(array('yz_shop_order.id'=>$id))->find();
       $shop=M('shop_item')->where("id=".$data['order']['item_id'])->find();
       $data['order']['title']=$shop['title'];
       $data['title']="订单处理";
       $this->assign($data);
       $this->display();
    }
    

    private function user_ticheng($order)
    {
        $M=M("user");
        $M->startTrans();
        $add_user_price = $M->lock('true')->where("id=".$order['uid'])->setInc("balance",$order['suk_ticheng']); //于额表加款
        if($add_user_price){
            $M->commit();//成功则提交
            //加款日志
           if(C("JIAKUAN_LOG")){
                $jiakuan_data=array(
                "uid"=>$order['uid'],
                'time'=>time()+rand(3,10),
                'price'=>$order['suk_ticheng'],
                'type'=> 1,
                'jifei_log_id' =>$order['ip'],
                "jiakuan_log_id" =>$order['item_id'],
                'info' =>"推广商品ID:".$order['item_id']."提成加款!",
                );
             M("jiakuan_log")->add($jiakuan_data);
            }
            $this->success("已结算!返款完成!");


         }else{
            $M->rollback();//不成功，则回滚
            YZLOG("加款事务处理不成功!查看往上日志");
            $this-error("返款处理失败");
        }
    }

    
    


}